﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HZU.ZP.Domain.IService;
using HZU.ZP.Domain.Model;
using HZU.ZP.Domain.IService.Resume;
using HZU.ZP.Domain.Model.Resume;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace HZU.ZP.Controllers
{
    public class CommentController : Controller
    {
        private ICompanyService _companyService;
        private IApplicantService _applicantService;
        private IInformationService _informationService;
        private ICommentService _commentService;
        public CommentController(ICompanyService companyService, IApplicantService applicantService,
            IInformationService informationService, ICommentService commentService)
        {
            _companyService = companyService;
            _applicantService = applicantService;
            _informationService = informationService;
            _commentService = commentService;
        }
        // GET: /<controller>/
        public IActionResult Index()
        {
            return View();
        }

        [Authorize]
        [HttpPost]
        public IActionResult AddComment([FromBody]Comment comment)
        {
            //判断评论人身份
            Information information = new Information();            
            switch (User.Claims.ToList()[2].Value)
            {
                case "Admin":
                    comment.OwnerName = "管理员";
                    break;
                case "Company":
                    comment.OwnerName = _companyService.GetByEmail(comment.OwnerName).CompanyName;
                    break;
                case "Applicant":
                    {
                        var app = _applicantService.GetApplicant(comment.OwnerName);
                        if (app != null)
                        {
                            information = _informationService.GetInformation(app.Id.Value);
                            if (information != null)
                                comment.OwnerName = information.Name;
                        }
                    }
                    break;
            }
            comment.CreateTime = DateTime.Now;
            _commentService.Add(comment);
            return Json(AjaxResult.ReturnMessage(1, "评论成功"));
        }

        [Authorize]
        [HttpGet]
        public IActionResult GetNoticeComment(int noticeId, int pageIndex = 1)
        {
            IList<Comment> comments = new List<Comment>();
            comments = _commentService.GetByNoticeId(noticeId, pageIndex);
            int pageCount = (int)Math.Ceiling((double)_commentService.GetByNoticeCount(noticeId) / 10);
            ViewData["PageCount"] = pageCount;
            if (pageCount == 0)
            {
                ViewData["PageIndex"] = 0;
            }
            else
            {
                ViewData["PageIndex"] = pageIndex;
            }

            return Json(AjaxResult.ReturnMessage(1, "成功", comments));
        }

        public IActionResult GetComComment(int commentId)
        {
            IList<Comment> comments = new List<Comment>();
            comments = _commentService.GetByCommentId(commentId);

            return Json(AjaxResult.ReturnMessage(1, "成功", comments));
        }
    }
}
